package com.itheima.web.service.impl.sms;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.exception.CustomException;
import com.itheima.result.PageResult;
import com.itheima.service.sms.SmsHomeAdvertiseService;
import com.itheima.vo.sms.SmsHomeAdvertise;
import com.itheima.web.mapper.sms.SmsHomeAdvertiseMapper;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;

import java.time.LocalDateTime;

@DubboService
public class SmsHomeAdvertiseServiceImpl extends ServiceImpl<SmsHomeAdvertiseMapper, SmsHomeAdvertise> implements SmsHomeAdvertiseService {
    @Override
    public void create(SmsHomeAdvertise smsHomeAdvertise) {

        this.save(smsHomeAdvertise);

    }

    @Override
    public PageResult selectPage(String name, String endTime, Integer pageNum, Integer pageSize, Integer type) {
        //构造分页构造器
        Page<SmsHomeAdvertise> pageInfo = new Page<>(pageNum, pageSize);
        //构造条件构造器
        LambdaQueryWrapper<SmsHomeAdvertise> lqw = new LambdaQueryWrapper<>();
        lqw.eq(type != null, SmsHomeAdvertise::getType, type).like(StringUtils.isNotEmpty(name), SmsHomeAdvertise::getName, name);
        lqw.ge(endTime != null, SmsHomeAdvertise::getEndTime, endTime);
        this.page(pageInfo, lqw);




        return PageResult.init(pageInfo.getTotal(),  Long.valueOf(pageNum),Long.valueOf(pageSize), pageInfo.getRecords());


    }

    @Override
    public void updateStatus(Long id, Integer status) {
        LambdaUpdateWrapper<SmsHomeAdvertise> luw = new LambdaUpdateWrapper<>();
        luw.set(SmsHomeAdvertise::getStatus, status).eq(SmsHomeAdvertise::getId, id);
        this.update(luw);
    }


}
